因为淘宝网需要登录,而京东的html比较奇怪,所以最后选取了当当网
技术路线
requests——re
#CrowDangDangPrize.py
import requests
import re
import urllib
def getHTMLText(url):
try:
r=requests.get(url,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
return ""
def parsePage(ls,html):
try:
pls=re.findall(r'now\_price\"\>\¥\;[\d\.]+',html)
nls=re.findall(r"alt\=\'.*?\'",html)
for i in range(len(pls)):
price=eval(pls[i].split(';')[1])
name=eval(nls[i].split("=")[1])
ls.append([price,name])

本文介绍了如何使用Python爬虫从当当网抓取商品比价信息。由于淘宝网需要登录,京东的HTML结构复杂,选择当当网作为目标。通过requests库获取网页源代码,再利用正则表达式解析商品名称和价格。每页有60个商品,通过re.findall()获取数据并处理列表内容。此外,还提到了在搜索过程中对文字进行gb2312编码的重要性。
最低0.47元/天 解锁文章

816

被折叠的 条评论
为什么被折叠?



