最近在学习Python爬虫程序,工具是pycharm,Python3.6。
因为我最终需要的是文本内容和文本标题所以实验以获取新浪新闻的文本和标题为例。
实现过程中加载的模块包括以下:
需要注意的是pip版本应当更新为10.0.1
完整代码如下:
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import re
import json
import pandas
def getNews(newsurl):
res = requests.get(newsurl)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
title = soup.select('title')[0].text
article = soup.find(id="article").get_text();
return title, article
def parseListLinks():
urlFormat = 'http://api.roll.news.sina.com.cn/zt_list?channel=news&cat_1=gnxw&cat_2==gdxw1||=gatxw||=zs-pl||=mtjj&level==1||=2&show_ex