目标
这次的目标是爬取北斗相关的新闻,利用图片来解释一些我们要做的事情
从左边的网页信息中获取子网页的网址,然后从子网页的网址中获取文章文本信息。接下来我们看一下子网页的网址在原网页的哪里
大家有没有注意到上一张图的网址的后面跟这张图所指的网址是一样的。所以就是这里
现在开始码代码,我使用的解析方法是beautifulsoup。
加载包
import requests
from bs4 import BeautifulSoup
import numpy as np
import pandas as pd
import json
import csv
获取初始网页信息
url='http://www.beidou.gov.cn/zt/zcfg/index.html'
headers={
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh,en-US;q=0.9,en;q=0.8",
"Connection":"keep-alive",
"Cookie":"_trs_uv=kfho2reo_1428_9fvp; _trs_ua_s_1=kfhtldgp_1428_f4iw",
"Host":"www.beidou.gov.cn",
"Referer":"http://www.beidou.gov.cn/zt/zcfg/index.html",
"Upgrade-Insecure-Requests":"1",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"
} #标头
req=requests.get(url,headers=headers)#请求
req.encoding='utf-8'#修改编码
html=req.text#提取网页文本内容
soup=BeautifulSoup(html,'lxml') #利用beautifulsoup解析
a_labels=soup.find_all(attrs={'class':'xiazaiLA'})#搜索class为xiazaiLA的网址源码