Python爬虫怎么抓取html网页的代码块
比如在这段代码中,我想抓取tablealign="center"border=7class="base_index">到<tdwidth="25%"class="head...
比如在这段代码中,我想抓取table align="center" border=7 class="base_index">到<td width="25%" class="head_td top_bottom"><a href="MinGe/Index.htm">民歌民乐</a></td>的内容,先不考虑编码的问题,为什么我用body = re.findall('<table align="center" border=7 class="base_index">(.*?)<td width="25%" class="head_td top_bottom"><a href="MinGe/Index.htm">',code,re.S)抓取之后输出的是一个list元组呢?
展开
2个回答
展开全部
mport urllib.request
import re
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
html = html.decode('GBK')
return html
def getMeg(html):
reg = re.compile(r'******')
meglist = re.findall(reg,html)
for meg in meglist:
with open('out.txt',mode='a',encoding='utf-8') as file:
file.write('%s\n' % meg)
if __name__ == "__main__":
html = getHtml(url)
getMeg(html)
import re
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
html = html.decode('GBK')
return html
def getMeg(html):
reg = re.compile(r'******')
meglist = re.findall(reg,html)
for meg in meglist:
with open('out.txt',mode='a',encoding='utf-8') as file:
file.write('%s\n' % meg)
if __name__ == "__main__":
html = getHtml(url)
getMeg(html)
展开全部
范围匹配大点,像这种
re.findall('(<div class="moco-course-wrap".*?</div>)',source,re.S)
可以看下这个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询