我想使用asyncio获取网页html。
我在jupyter笔记本中运行以下代码:
import aiofiles
import aiohttp
from aiohttp import ClientSession
async def get_info(url, session):
resp = await session.request(method="GET", url=url)
resp.raise_for_status()
html = await resp.text(encoding='GB18030')
with open('test_asyncio.html', 'w', encoding='utf-8-sig') as f:
f.write(html)
return html
async def main(urls):
async with ClientSession() as session:
tasks = [get_info(url, session) for url in urls]
return await asyncio.gather(*tasks)
if __name__ == "__main__":
url = ['http://huanyuntianxiazh.fang.com/house/1010123799/housedetail.htm', 'http://zhaoshangyonghefu010.fang.com/house/1010126863/housedetail.htm']
result = asyncio.run(main(url))
Run Code Online (Sandbox Code Playgroud)
但是,它返回 RuntimeError: asyncio.run() cannot be called from a running event loop
问题是什么?
怎么解决呢?
非常感谢你。
我可以使用cv2读取jpg文件
import cv2
import numpy as np
import urllib
url = r'http://www.mywebsite.com/abc.jpg'
req = urllib.request.urlopen(url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr,-1)
cv2.imshow('abc',img)
Run Code Online (Sandbox Code Playgroud)
但是,当我使用gif文件时,它会返回一个错误:
error: (-215) size.width>0 && size.height>0 in function cv::imshow
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题呢?
使用正则表达式时,我得到:
import re
string = r'http://www.example.com/abc.html'
result = re.search('^.*com', string).group()
Run Code Online (Sandbox Code Playgroud)
在大熊猫中,我写道:
df = pd.DataFrame(columns = ['index', 'url'])
df.loc[len(df), :] = [1, 'http://www.example.com/abc.html']
df.loc[len(df), :] = [2, 'http://www.hello.com/def.html']
df.str.extract('^.*com')
ValueError: pattern contains no capture groups
Run Code Online (Sandbox Code Playgroud)
如何解决问题?
谢谢。
I have a dataframe
df = pd.DataFrame(columns = ['id', 'name', 'age'])
df.loc[len(df), :] = [1, 'John', 21]
df.loc[len(df), :] = [2, 'Mary', 19]
df.loc[len(df), :] = [3, 'Ann', 27]
df.loc[len(df), :] = [4, 'Ben', 18]
Run Code Online (Sandbox Code Playgroud)
I want to save it to excel file using xlsxwriter.
However, I want the age larger than 20 to be in red.
In other words, 21 and 27 should appear red in the excel file.
How to do it?
我有一个数据帧,df:
id_0 id_1 id_2
0 1 0 1
1 1 0 0
2 0 1 0
3 1 1 0
4 0 0 1
5 0 0 0
Run Code Online (Sandbox Code Playgroud)
我想获得每行的列名称,如果有1.如何做到这一点?谢谢.
结果:
result
0 id_0, id_2
1 id_0
2 id_1
3 id_0, id_1
4 id_2
5 NaN
Run Code Online (Sandbox Code Playgroud) 我想用 Python 读/写 Google Cloud Storage 存储桶中的文件。
假设我在gs://my_project/data.
如何列出上述文件夹中的文件夹和文件?
如何读写文件?
如何在关键字之后提取子字符串am,is或are从字符串中提取但不包含am,is或are?
string = 'I am John'
Run Code Online (Sandbox Code Playgroud)
我用了:
re.findall('(?<=(am|is|are)).*', string)
Run Code Online (Sandbox Code Playgroud)
发生错误
re.error: look-behind requires fixed-width pattern
Run Code Online (Sandbox Code Playgroud)
正确的做法是什么?
我想显示张量的值。
这是我的代码:
#some code here
data = [data_tensor for data_tensor in data_dict.items()]
for i in data:
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print (sess.run(i[1]))
print('_'*100)
Run Code Online (Sandbox Code Playgroud)
但是,我得到了错误:
FailedPreconditionError (see above for traceback):
GetNext() failed because the iterator has not been initialized.
Ensure that you have run the initializer operation for this iterator
before getting the next element.
Run Code Online (Sandbox Code Playgroud)
如何解决问题?
非常感谢你。
某些网站会自动播放视频或放大图像。如何使用Selenium和Python停止它?
非常感谢你。
python ×8
dataframe ×2
pandas ×2
javascript ×1
opencv ×1
python-3.x ×1
regex ×1
selenium ×1
tensorflow ×1