鱼C论坛

 找回密码
 立即注册
查看: 1382|回复: 5

[已解决]为啥不打印啊

[复制链接]
发表于 2020-7-27 22:24:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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()
最佳答案
2020-7-27 22:48:25


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

  1. import requests
  2. import bs4


  3. def open_url(url):
  4.     headers = {
  5.         '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'}
  6.     res = requests.get(url, headers=headers)
  7.     return res


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


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


  18. if __name__ == '__main__':
  19.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-27 22:30:21 | 显示全部楼层
求解答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-27 22:48:25 | 显示全部楼层    本楼为最佳答案   


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

  1. import requests
  2. import bs4


  3. def open_url(url):
  4.     headers = {
  5.         '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'}
  6.     res = requests.get(url, headers=headers)
  7.     return res


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


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


  18. if __name__ == '__main__':
  19.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-28 02:48:48 | 显示全部楼层
target = content.find_all("p", style="TEXT-INDENT: 2em")#你换上我这句就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

应该养成习惯,每一步都print调试一下,,别中间都没成功,就给直接写到后面了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-28 10:16:33 | 显示全部楼层
luxiaolan6373 发表于 2020-7-28 02:49
应该养成习惯,每一步都print调试一下,,别中间都没成功,就给直接写到后面了

好的,感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 08:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表