Pythonnewers 发表于 2020-5-11 15:42:05

获取每个词语的意思

爬取百度百科的词语来搞事情
import requests
import bs4
import time
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.14 Safari/537.36 Edg/83.0.478.13"}


def req(name):
    url = "https://baike.baidu.com/item/" + name
    time.sleep(1)
    print("Loading...")
    html = requests.get(url=url, headers=headers).content.decode("utf-8")
    soup = bs4.BeautifulSoup(html, "html.parser")
    try:
      string = soup.find(class_="para").get_text()
      print(string)
    except AttributeError:
      print("没有找到这个词!")


if __name__ == "__main__":
    while True:
      name = input("输入需要的词:")
      time.sleep(1)
      print("Loading.")
      req(name)

@不二如是 回复可见我是不可以设置吗

MIke_python小小 发表于 2020-5-11 15:48:46

你很NB呀
页: [1]
查看完整版本: 获取每个词语的意思