題目連接php
import requests
from bs4 import BeautifulSoup
r = requests.get('http://ctf5.shiyanbar.com/jia/index.php')
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text,'html.parser')
a = soup.find_all('div',{"name":"my_expr"})
只有一個p標籤
因此能夠直接這麼使用直接匹配phtml
b=soup.p.div.get_text()
題目地址python
解析出排名和大學名稱便可app
from bs4 import BeautifulSoup
import requests
r = requests.get('http://www.zuihaodaxue.cn/zuihaodaxuepaiming2019.html')
r.encoding = r.apparent_encoding
attr = {"class":"table table-small-font table-bordered table-striped"}
soup = BeautifulSoup(r.text,'html.parser')
a = soup.find('table',attr)
a = a.find_all('tr',{"class":"alt"})
result = '排名,大學\n'
for i in a:
result += i('td')[0].text +','+i.div.text+'\n'
with open('rank.csv','w') as f:
f.write(result)