weixin_47234335 2021-10-20 00:23 采纳率: 100%
浏览 35
已结题

采用BeautifulSoup库无法解析到网址信息,求解决方法

网址源码如下:

<span class="company-page-item-right cmpweb">
 <a target="_blank" href="http://www.gmgitc.com">www.gmgitc.com</a>
</span>

通过BS解析后,无法获取a标签内容

import requests
from bs4 import BeautifulSoup

intro_url = 'https://xinsanban.eastmoney.com/F10/CompanyInfo/Introduction/831039.html'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\
            /537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'}
resp = requests.get(intro_url, headers=headers, timeout=1)
print(bs)
___________________________________________________________________________________________________
<li class="even">
<span class="company-page-item-left ">公司网址</span>
<span class="company-page-item-right cmpweb"/>
</li>



  • 写回答

2条回答 默认 最新

  • 关注

    因为这个网页中的公司网址是通过js代码来动态更新的。
    requests只能获取网页的静态源代码,动态更新的内容取不到。
    对于动态更新的内容要用selenium 来爬取。

    在页面上点击右键,右键菜单中选 "查看网页源代码"。

    img


    这样看到的才是网页的静态源代码。

    img

    可以看到这个网页的静态源代码中没有你需要的<a target="_blank" href="http://www.gmgitc.com">www.gmgitc.com</a>
    说明该页面的内容是动态更新的,要用selenium 来爬取,比如:.

    from selenium import webdriver
    import time
    from bs4 import BeautifulSoup
    driver = webdriver.Chrome()
    intro_url = 'https://xinsanban.eastmoney.com/F10/CompanyInfo/Introduction/831039.html'
    driver.get(intro_url)
    time.sleep(3)
    html = driver.page_source
    soup=BeautifulSoup(html,'lxml')
    req=soup.select('span.company-page-item-right.cmpweb')[0]
    print(req)
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月28日
  • 已采纳回答 10月20日
  • 创建了问题 10月20日

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?