【发布时间】:2021-11-15 16:13:01
【问题描述】:
我有一个抓取 url 列表的功能,200k url,花了很多时间,有什么方法可以加快这个过程?
def get_odds(ids):
headers = {"Referer": "https://www.betexplorer.com",
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'}
s = requests.Session()
matches=[]
for id in ids:
url = f'https://www.betexplorer.com{id}'
response = s.get(url, headers=headers)
soup = BeautifulSoup(response.text,'html.parser')
season = url.split('/')[5]
"do stuff.."
ids 是list
['/soccer/england/premier-league/brentford-norwich/vyLXXRcE/'
...]
【问题讨论】:
-
第一步是使用分析器来衡量花费最多的时间。
-
每个网址需要 1-2 秒,但乘以 200k 是很多时间..
标签: python web-scraping beautifulsoup python-requests