【问题标题】:Python Web Scraping 'None'Python Web 抓取“无”
【发布时间】:2020-08-04 13:05:02
【问题描述】:

我正在尝试从网站https://10times.com/losangeles-us/technology/conferences 抓取事件数据。

下面的代码会继续抓取在主登录页面上找到的事件数据和事件链接。

import pandas as pd
import bs4 as bs
import urllib.request

source = urllib.request.urlopen('https://10times.com/losangeles-us/technology/conferences').read()
soup = bs.BeautifulSoup(source,'html.parser')

aa = []
bb = []

#---Get Event Data---
table = soup.find('tbody')
table_rows = table.find_all('tr') #find table rows (tr)
for x in table_rows:   
    data = x.find_all('td')  #find table data
    row = [x.text for x in data]
    if len(row) > 2: #Exlcudes rows with only event name/link, but no data.
        aa.append(row)
df_event = pd.DataFrame(aa, columns=['Date', 'Event Name', 'Venue', 'Description', 'Type', 'Unnamed:'])
df_event.columns = ['Date', 'Event Name', 'Venue', 'Description', 'Type', 'Interested/Following Count']

#---Get Links---
h2 = soup.find_all('h2')
for i in h2:
    links = i.a['href']
    bb.append(links)
df_link = pd.DataFrame(bb)
df_link.columns = ['Links']

#---Combines dfs---#
df = pd.concat([df_event,df_link],sort=False, axis=1)
df.index += 1       

#--Export to HTML---
df.to_html('10times_Scrape.html',render_links=True)

我现在想转到我提取的每个事件链接,并在页面上找到地址/完整事件描述。 示例链接: https://10times.com/microsoft-business-applications-summit-anaheim

事件描述和地址都可以在 P 标签中找到。但是,当我阅读链接时,我只是返回 None's .. 我在下面使用正确的 div 类?我究竟做错了什么?我想查看 'class':'col-md-6' 并提取地址。

#---Get Address---
for i in bb:
    soup2 = bs.BeautifulSoup(i, 'html.parser')
    text2 = soup2.find('div', attrs={'class':'col-md-6'})
    print(text2)

【问题讨论】:

    标签: python python-3.x pandas beautifulsoup jupyter-notebook


    【解决方案1】:

    好像你错过了urllib 获取内部链接。

    #---Get Address---
    for i in bb:
        inner_source = urllib.request.urlopen(i).read()
    
        soup2 = bs.BeautifulSoup(inner_source, 'html.parser')
        text2 = soup2.find('div', 'col-md-6')
        print(text2)
    

    还可以使用find/find_all 传递一个类作为第二个位置参数。并且只是为了强制 find 只会返回第一次出现,即使有很多。

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2018-06-13
      • 2021-07-29
      • 2021-04-20
      • 2021-03-16
      • 2017-11-23
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多