Hant 发表于 2020-7-27 22:24:23

为啥不打印啊

import requests
import bs4

def open_url(url):
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0'}
    res = requests.get(url,headers=headers)
    return res

def find_data(res):
    soup=bs4.BeautifulSoup(res.text,'html.parser')
    content = soup.find(id='Cnt-Main-Article-QQ')
    target=content.find_all("P",style="TEXT-INDENT: 2em")
    for each in target:
      print(each)



def main():
    url=('https://news.house.qq.com/a/20170702/003985.htm')
    res=open_url(url)
    find_data(res)
   
if __name__=='__main__':
    main()

Hant 发表于 2020-7-27 22:30:21

求解答

Twilight6 发表于 2020-7-27 22:48:25

Hant 发表于 2020-7-27 22:30
求解答


你的 P 是大写的 ,但是网站节点是小写的 p

import requests
import bs4


def open_url(url):
    headers = {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0'}
    res = requests.get(url, headers=headers)
    return res


def find_data(res):
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    content = soup.find(id='Cnt-Main-Article-QQ')
    target = content.find_all("p",style="TEXT-INDENT: 2em")
    for each in target:
      print(each)


def main():
    url = ('https://news.house.qq.com/a/20170702/003985.htm')
    res = open_url(url)
    find_data(res)


if __name__ == '__main__':
    main()

luxiaolan6373 发表于 2020-7-28 02:48:48

target = content.find_all("p", style="TEXT-INDENT: 2em")#你换上我这句就行了

luxiaolan6373 发表于 2020-7-28 02:49:55

luxiaolan6373 发表于 2020-7-28 02:48
target = content.find_all("p", style="TEXT-INDENT: 2em")#你换上我这句就行了

应该养成习惯,每一步都print调试一下,,别中间都没成功,就给直接写到后面了

Hant 发表于 2020-7-28 10:16:33

luxiaolan6373 发表于 2020-7-28 02:49
应该养成习惯,每一步都print调试一下,,别中间都没成功,就给直接写到后面了

好的,感谢
页: [1]
查看完整版本: 为啥不打印啊