代码之家  ›  专栏  ›  技术社区  ›  Han Zhengzu

使用python获取html内容中的内容

  •  2
  • Han Zhengzu  · 技术社区  · 6 年前

    中国网站 here 主要描述一家公司的信息。由于有许多页面包含类似的内容,我决定学习python中的数据爬虫。

    基本代码

    import requests
    from bs4 import BeautifulSoup
    page = requests.get('http://182.148.109.184/enterprise- 
    info!getCompanyInfo.action?companyid=1000356')
    
    soup = BeautifulSoup(page.text, 'html.parser')
    source_content = soup.find(class_='rightSide').find(class_='content register').find(class_='formestyle')
    

    我想收集的信息

    这个图是在chrome的inspect元素页面中捕获的。

    enter image description here

    也许中国人在这里不友好,我在这里创造了一个更好的例证。

    <th> the variable name </th> => For example, "company name", "company location"
    <td> the target data I want to save </td>
    

    我的问题

    根据我的基本代码, source_content 里面没有任何信息。输出文件如下所示:

    enter image description here

    对比图1,2,我们可以看到经度,纬度的信息已经消失了。

    如何用python获取这些数据?任何建议都将不胜感激

    1 回复  |  直到 6 年前
        1
  •  1
  •   Martin Evans    6 年前

    如果您提供 Referer 请求中的标题如下:

    import requests
    from bs4 import BeautifulSoup
    
    url = 'http://182.148.109.184/enterprise-info!getCompanyInfo.action?companyid=1000356'
    page = requests.get(url, headers={'Referer' : url})
    soup = BeautifulSoup(page.text, 'html.parser')
    
    table = soup.find(class_='formestyle')
    
    for tr in table.find_all('tr'):
        row = [v.text for v in tr.find_all(['th', 'td'])]
        print(row)
    

    这将显示以下类型的数据:

    ['地理坐标:', '经度:104.2153 \xa0\xa0纬度:31.3631']
    

    如您所见,信息现在已存在。