相信考托的老铁们都知道题目的可贵,在这里跟大家分享一下托福tpo爬虫,这里爬取的是阅读。
首先设置请求头。ua自行在chrome开发者复制粘贴,这里不赘述。
headers = {
'Host':'top.zhan.com',
'Referer':'http://top.zhan.com/toefl/speak/task12.html',
'User-Agent':'',
}
接下来,定义请求页面函数。第一个参数是题目总页面数值,第二个参数是小题分页面数值。
def get_page(num,q_num):
url = f'http://top.zhan.com/toefl/read/practicereview-{num}-13-0-{q_num}.html'
try:
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
except requests.ConnectionError as e:
print('Error', e.args)
请求搞定了,就要定义阅读文章获取函数,因为只想获取一遍,所以单独拿出来定义二没有放入循环。这里因为此网站的html解析时有一小部分驴唇不对马嘴,所以用了各种花样来获取相关值。
def get_article(text, q_num):
doc = pq(text)
if q_num &#