看了嵩天教授的【Python网络爬虫与信息提取】.MOOC. 北京理工大学 课程,里面有一段演示如何从淘宝爬取价格信息,但实际操作却不行,问题在于淘宝19年开始实行搜索必须登录,但是Python爬取该如何做呢?
先上完整代码:
import requests
import re
def getHTMLText(url): #获得页面函数,淘宝需要登录验证,暂时访问不了
try:
header={
'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'cookie':'thw=cn; tracknick=***;*************************************************************************3fP.'
}
r=requests.get(url,headers=header,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
# print(r.text)
return r.text
except:
return ""
def parsePage(ilt,html): #解析获得的页面
try:
plt=re.findall(r'\"view_price\"\:\"[\d\.]*"',html)
tlt=re.findall(