from urllib import request
from bs4 import BeautifulSoup

url = r'http://www..com/'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
page = request.Request(url, headers=headers)
page_info = request.urlopen(page).read().decode('utf-8')
soup = BeautifulSoup(page_info, 'html.parser')
titles = soup.find_all('h1', 'post-title')
for title in titles:
    print(title.string + '\n')
try:
    file = open('titles.txt', 'w')
    for title in titles:
        file.write(title.string + '\n')
finally:
    if file:
        file.close()

最后修改:2019 年 12 月 18 日 09 : 47 PM
如果觉得我的文章对你有用,请随意赞赏