【发布时间】:2021-01-15 08:28:06
【问题描述】:
我遇到了旧的工作代码无法正常运行的问题。
我的 python 代码正在使用漂亮的汤抓取网站并提取事件数据(日期、事件、链接)。
我的代码正在提取位于tbody 中的所有事件。每个事件都存储在<tr class="Box"> 中。问题是我的抓取工具似乎在此<tr style ="box-shadow: none;> 之后停止在它到达此部分(这是一个包含网站上我不想抓取的事件的 3 个广告的部分)代码停止从内部提取事件数据<tr class="Box">。有没有办法跳过这种 tr 风格/忽略未来的案例?
import pandas as pd
import bs4 as bs
from bs4 import BeautifulSoup
import urllib.request
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module='bs4')
source = urllib.request.urlopen('https://10times.com/losangeles-us/technology/conferences').read()
soup = bs.BeautifulSoup(source,'html.parser')
#---Get Event Data---
test1=[]
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.
test1.append(row)
test1
【问题讨论】:
标签: python-3.x python-2.7 web-scraping beautifulsoup