【问题标题】:Parsing a table using beautifulsoup使用 beautifulsoup 解析表格
【发布时间】:2017-02-15 02:37:41
【问题描述】:

想要在每次更新时获取表的内容。使用美丽汤。为什么这段代码不起作用?它有时不返回任何输出或抛出异常

from bs4 import BeautifulSoup
import urllib2
url =    "http://tenders.ongc.co.in/wps/portal/!ut/p/b1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOINLc3MPB1NDLwsPJ1MDTzNPcxMDYJCjA0MzIAKIoEKDHAARwNC-sP1o8BK8Jjg55Gfm6pfkBthoOuoqAgArsFI6g!!/pw/Z7_1966IA40J8IB50I7H650RT30D2/ren/m=view/s=normal/p=struts.portlet.action=QCPtenderHomeQCPlatestTenderListAction/p=struts.portlet.mode=view/=/#Z7_1966IA40J8IB50I7H650RT30D2"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page)
divcontent = soup.find('div', {"id":"latestTrPagging", "class":"content2"})
table = soup.find_all('table')
rows = table.findAll('tr', {"class":"even", "class": "odd"})
for row in rows:
    cols = row.findAll('td', {"class":"tno"})
for td in cols:
    print td.text(text=True)`

网址是https://tenders.ongc.co.in/wps/portal/!ut/p/b1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOINLc3MPB1NDLwsPJ1MDTzNPcxMDYJCjA0MzIAKIoEKDHAARwNC-sP1o8BK8Jjg55Gfm6pfkBthoOuoqAgArsFI6g!!/pw/Z7_1966IA40J8IB50I7H650RT30D2/ren/m=view/s=normal/p=struts.portlet.action=QCPtenderHomeQCPlatestTenderListAction/p=struts.portlet.mode=view/=/#Z7_1966IA40J8IB50I7H650RT30D2 只想获取表格部分并在有新投标时得到通知

【问题讨论】:

    标签: python-2.7 web-scraping beautifulsoup html-parsing


    【解决方案1】:

    这对我有用 - 使用 requests 而不是 urllib2,设置 User-Agent 标头并调整一些定位器:

    from bs4 import BeautifulSoup
    import requests
    
    
    url = "https://tenders.ongc.co.in/wps/portal/!ut/p/b1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOINLc3MPB1NDLwsPJ1MDTzNPcxMDYJCjA0MzIAKIoEKDHAARwNC-sP1o8BK8Jjg55Gfm6pfkBthoOuoqAgArsFI6g!!/pw/Z7_1966IA40J8IB50I7H650RT30D2/ren/m=view/s=normal/p=struts.portlet.action=QCPtenderHomeQCPlatestTenderListAction/p=struts.portlet.mode=view/=/#Z7_1966IA40J8IB50I7H650RT30D2"
    page = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"})
    soup = BeautifulSoup(page.content, "html.parser")
    
    divcontent = soup.find('div', {"id": "latestTrPagging", "class": "content2"})
    table = soup.find('table')
    rows = table.find_all('tr', {"class": ["even", "odd"]})
    
    for row in rows:
        cols = row.find_all('td', {"class": "tno"})
        for td in cols:
            print(td.get_text())
    

    打印前 10 个投标号码:

    LC1MC16044[NIT]
    LC1MC16043[NIT]
    LC1MC16045[NIT]
    EY1VC16028[NIT]
    RC2SC16050(E -tender)[NIT]
    RC2SC16048(E -tender)[NIT]
    RC2SC16049(E -tender)[NIT]
    UI1MC16002[NIT]
    V16RC16015[E-Gas]
    K16AC16002[E-Procurement]
    

    请注意您应该如何处理多个类(“偶数”和“奇数”)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2011-05-10
      • 2011-01-04
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2020-02-25
      相关资源
      最近更新 更多