爬取搜狗热门游戏榜单

1.打开网站:http://top.sogou.com/game/quanbu_1.html(搜狗热门游戏榜单

 

 

 

                                                                                                          

 

2.打开网页源代码,爬取需要内容:

 

 

 

 3.导入相应数据库,利用代码获取信息。

import requests
import pandas as pd
from bs4 import BeautifulSoup
from pandas import DataFrame
url="http://top.sogou.com/game/quanbu_1.html"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/69.0.3497.100 Safari/537.36'}
r=requests.get(url)
r.encoding=r.apparent_encoding
html = r.text
soup = BeautifulSoup(html,'lxml')
title=[]
heat=[]
for m in soup.find_all(class_="p1"):
title.append(m.get_text().strip())
for n in soup.find_all(class_="s3"):
heat.append(n.get_text().strip())
data=[title,heat]
print(data)
a=pd.DataFrame(data,index=["标题","热度"])
print(a.T)

4.获得数据

 

posted @ 2020-03-21 16:39  somde  阅读(244)  评论(0编辑  收藏  举报