Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

需要在get_one_page函数里加个response.close() #6

Open
krookies opened this issue Apr 8, 2021 · 3 comments
Open

需要在get_one_page函数里加个response.close() #6

krookies opened this issue Apr 8, 2021 · 3 comments

Comments

@krookies
Copy link

krookies commented Apr 8, 2021

def get_one_page(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
response.close()
return response.text
return None
except RequestException:
return None

@mrc321
Copy link

mrc321 commented Apr 14, 2021

不加会怎样?

@krookies
Copy link
Author

加上这个response.close()爬取速度会明显加快,不加的话,你的request请求不会立即关闭,容易被网站侦测出来,可以对比一下试试

@hikaruwin
Copy link

可不可以用上下文方式写,with requrests.get这种的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants