Python抓取网页信息并存储到excel中

目的:抓取网页上的人物和相关简介信息
代码分享:
import urllib.request
import ssl
ssl._create_default_https_context=ssl._create_unverified_context
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
html = html.decode(‘utf-8’)
return html

if name == “main”:
# from requests_html import HTMLSession
# import requests
import pandas as pd
import bs4
import re
# testurl1 = “http://dtcc.it168.com/2010/
url_typ1_list = [“http://dtcc.it168.com/2010/",“http://dtcc.it168.com/2011/”,“http://dtcc.it168.com/2012/”,“http://dtcc.it168.com/2013/”,“http://dtcc.it168.com/2014/”,“http://dtcc.it168.com/2015/”,“http://dtcc.it168.com/2016/”,"http://dtcc.it168.com/2017/”]
result_list = []
for url in url_typ1_list:
try:
html_info = getHtml(url)
soup = bs4.BeautifulSoup(html_info, ‘html.parser’)
info_list = soup.select(".yjjb")
for info in info_list:
tmp = info.contents[-1]
if len(tmp) < 2:
continue
# try:
# tmp = str(tmp)
# pat = re.compile(’>(.*?)<’)
# tmp = ‘’.join(pat.findall(tmp))
# except:
# tmp = tmp
try:
info = tmp.strip()
except:
info = tmp
try:
info = info.split(":")
if len(info) == 2:
result_list.append(info)
except:
continue
except Exception as e:
print("grab web info Error, url=%s, Exception=%s. " % (url, e))
print(result_list)
print(len(result_list))
df = pd.DataFrame(result_list)
df.columns = [‘名字’,‘简介’]
df.to_csv(‘网页人物信息抓取类型1.csv’, encoding=‘gbk’, index=False)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值