【发布时间】:2019-04-07 06:14:01
【问题描述】:
此代码运行良好。但我想知道它是如何工作的。 谁能帮我解释一下这段代码??
scraper.py
from bs4 import BeautifulSoup
import requests
def scrape(url="https://www.onlinekhabar.com/2018/12/724699"):
try:
res = requests.get(url)
# print(res.text)
# print(res.encoding)
res.encoding = "utf-8"
bs = BeautifulSoup(res.text, "html.parser")
dict = {}
dict["title"] = bs.select(".nws__title--card > h2")[0].text
dict["published"] = bs.select(".post__time > span")[0].text
dict["description"] = bs.select(".main__read--content")[0].text
return dict
except:
return None
if __name__ == '__main__':
print(scrape())
【问题讨论】:
标签: django web-scraping