Python Requests库使用代理参数抓取Twitter数据
import requests import re from lxml import etree import os
fcpc_ersflist_gzcount
url="https://twitter.com/search?q=(%E5%A4%A7%E8%BF%90%E4%BC%9A%20OR%20FISU)%20until%3A2023-07-22%20since%3A2023-07-01&src=typed_query&f=live:7890" headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0"} r=requests.get(url=url,headers=headers) r.encoding = r.apparent_encoding
r.encoding="utf-8"
r=r.encoding
if not os.path.exists("./4kpiclibs"):
os.mkdir("./4kpiclibs")
#解析源码数据 tree=etree.HTML(r) div_list=tree.xpath('//div[@data-testid="cellInnerDiv"]') 报错:SSLError: HTTPSConnectionPool(host='twitter.com', port=443): Max retries exceeded with url: /search?q=(%E5%A4%A7%E8%BF%90%E4%BC%9A%20OR%20FISU)%20until%3A2023-07-22%20since%3A2023-07-01&src=typed_query&f=live:7890 (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)')))如何在请求中添加代理参数 为了解决抓取Twitter数据时出现的SSLError问题,可以使用代理服务器来发送请求。在Python Requests库中,可以通过proxies参数设置代理服务器。
proxies参数
proxies参数是一个字典,包含代理的相关信息,例如代理的类型、IP地址和端口号。
示例代码
import requests
url = "https:\/\/twitter.com\/search?q=(%E5%A4%A7%E8%BF%90%E4%BC%9A%20OR%20FISU)%20until%3A2023-07-22%20since%3A2023-07-01&src=typed_query&f=live:7890"
proxies = {
"http": "http:\/\/127.0.0.1:8080",
"https": "http:\/\/127.0.0.1:8080"
}
headers = {"User-Agent": "Mozilla\/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko\/20100101 Firefox\/113.0"}
r = requests.get(url=url, headers=headers, proxies=proxies)
r.encoding = r.apparent_encoding
content = r.text
在上述示例中,我们创建了一个proxies字典,其中包含了http和https的代理地址和端口号。然后,在发送请求时,将proxies参数设置为该字典。这样就可以通过代理发送请求了。
请注意,示例中的代理地址和端口号需要根据实际情况进行替换。
原文地址: https://www.cveoy.top/t/topic/pYUO 著作权归作者所有。请勿转载和采集!