使用REGEX在python中进行Web抓取

2024-09-24 22:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从亚马逊网站比如在python中使用web抓取的产品描述和产品细节。在

我有产品说明书。现在我需要产品细节。但这是非常困难的。在

我是网络抓取新手。帮我解决这个问题。在

到现在为止我的代码是

from bs4 import BeautifulSoup
import urllib2
import re
wiki = "http://www.amazon.in/gp/product/8129142147/ref=s9_ri_gw_g14_i1_r/253-2172200-7363412?pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=desktop-top&pf_rd_r=0B55B6ZXRHCN80WJGE0V&pf_rd_t=36701&pf_rd_p=0d2c13de-20bd-41c0-9412-48afb21e298f&pf_rd_i=desktop"
header = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(wiki, headers=header)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
h2 = soup.find("h2", {"class": "books"})
# print h2.get_text()
one = soup.find("div", {"id": "productDescription"})
# print one.get_text()
table = soup.findAll("table")
one2 = soup.find("td", {"class": "bucket"})
# print one2.get_text()
one3 = one2.find("h2")
# print one3.get_text()
one4 = one2.findAll("li")
# one4 = re.compile(r'(?<=>).*(?=<)')
# result = one4.findall(one4)[0]
# print result

产品详情见html表格

html是

^{pr2}$

请用示例代码帮助我获取内容。请使用REGEX(正则表达式),以便我可以通过示例了解它。在

注意:我使用的是python2.7

谢谢。在


Tags: 代码textimportget产品h2rdfind