picky_girl 发表于 2018-4-9 23:21:51

求助爬虫的一个问题

import urllib.request as ur
from bs4 import BeautifulSoup
import re
#抓取网页
url=r'http://www.100ppi.com/sf/day-2017-01-04.html'
header={'User-Agent':'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}
req=ur.Request(url=url,headers=header)
html=ur.urlopen(req).read().decode('utf-8')
soup=BeautifulSoup(html,'html.parser')
#取出表格
tab=soup.find_all('table')
trs=soup.find_all('tr',align='center')
trs1=str(trs)
#根据正则表达式抓取表格内容
res2=r'''<td><astyle="color:#21469f;" target="_blank">(.*)</a></td>\n<td>(.*)</td>\n<td>(.*)</td>\n<td>(.*)</td>\n<td>\n<table width="100%"><tr><td align="center" width="50%"><font color=.*>(.*)</font></td><td align="center" width="50%"><font color=.*>(.*)</font></td></tr></table>\n</td>\n<td>(.*)</td>\n<td>(.*)</td>\n<td>\n<table width="100%"><tr><td align="center" width="50%"><font color=.*>(.*)</font></td><td align="center" width="50%"><font color=.*>(.*)</font></td></tr></table>\n</td>\n</tr>, <tr align="center" bgcolor="#fafdff">'''
l=re.findall(res2,trs1,re.S|re.M)
最后结果

求问大神如何更改,我觉得是不是我正则表达式太复杂了

picky_girl 发表于 2018-4-10 00:22:42

额。已解决{:5_91:}

faschou 发表于 2018-4-12 11:45:44

picky_girl 发表于 2018-4-10 00:22
额。已解决

怎么解决的?
页: [1]
查看完整版本: 求助爬虫的一个问题