Created
May 9, 2018 02:33
-
-
Save ZhangZhongwei73671/a726dac2660911dbe530ebee62f110a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
link = "http://www.santostang.com/" | |
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36'} | |
r = requests.get(link, headers= headers) | |
soup = BeautifulSoup(r.text, "html.parser") | |
first_title = soup.find("h1", class_="post-title").a.text.strip() | |
print("第一篇文章标题是:", first_title) | |
title_list = soup.find_all("h1", class_="post-title") | |
for i in range(len(title_list)): | |
title = title_list[i].a.text.strip() | |
print("第%s篇文章的标题是:%s" %(i+1,title)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment