各位老师好,下面是一个网页,网页的格式分别是显示问题,然后是问题的解答,一个网页上总共有5道题和5个解答,如何写个简单的python爬虫程序,把问题和答案分别都爬下来,问题一列之中,然后答案放在另一个列之中,形成一个我二维表
网页内容如下:
https://mp.weixin.qq.com/s/Vt14hEa46W6QKljO6R0FBw
刚开始学习python,自己摸索了半天也没弄出来,希望老师能帮忙给解答下,非常感谢!
各位老师好,下面是一个网页,网页的格式分别是显示问题,然后是问题的解答,一个网页上总共有5道题和5个解答,如何写个简单的python爬虫程序,把问题和答案分别都爬下来,问题一列之中,然后答案放在另一个列之中,形成一个我二维表
网页内容如下:
https://mp.weixin.qq.com/s/Vt14hEa46W6QKljO6R0FBw
刚开始学习python,自己摸索了半天也没弄出来,希望老师能帮忙给解答下,非常感谢!
其实就是对网站获得的数据做一个整理,用正则表达式找出你想要的东西就行
from bs4 import BeautifulSoup
import requests
url="https://mp.weixin.qq.com/s/Vt14hEa46W6QKljO6R0FBw"
html=requests.get(url);
soup=BeautifulSoup(html.text,"html.parser");
question=[]
answer=[]
for it in soup.find_all("span",attrs={"style":"font-family: 微软雅黑;font-size: 16px;color: rgb(61, 170, 214);"}):
question.append(it.text)
for it in soup.find_all("section",attrs={"style":"padding-top: 5px;max-width: 100%;box-sizing: border-box;text-align: center;border-color: rgb(245, 245, 244);background-color: rgb(245, 245, 244);word-wrap: break-word !important;overflow-wrap: break-word !important;"}):
answer.append(it.text)
for i in range(len(question)):
print(question[i]+'\n')
print(answer[i]+'\n\n\n')