【问题标题】:Beautiful Soup returning nothing美丽的汤什么都不返回
【发布时间】:2013-05-30 19:22:20
【问题描述】:

您好,我正在为我的学校做一个涉及抓取 HTML 的项目。

但是,当我查找表格时,我没有返回任何内容。这是遇到问题的部分。

如果您需要更多信息,我很乐意提供给您

from bs4 import BeautifulSoup
import urllib2
import datetime

#This section determines the date of the next Saturday which will go onto the end of     the URL 
d = datetime.date.today() 
while d.weekday() != 5:
    d += datetime.timedelta(1)

#temporary logic for testing when next webpage isn't out
d = "2013-06-01"

#Section that scrapes the data off the webpage
url = "http://www.sydgram.nsw.edu.au/co-curricular/sport/fixtures/" + str(d) + ".php"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page)
print soup
#Section that grabs the table with stuff in it
table = soup.find('table', {"class": "excel1"})
print table

【问题讨论】:

  • (此评论离题)请加入chat.stackoverflow.com/rooms/6/python :)
  • 不,我是说我的评论离题了
  • 啊,干杯。不用担心:)
  • 你检查过pagesoup的内容吗?它们是您所期望的对象吗?此外,如果找不到表,您的搜索可能不正确。
  • @SimeonVisser 我已经打印了汤,这很好,但是我在 39497600 的“page”Addinfourl 得到了一个奇怪的响应,其 fp =

标签: python python-2.7 beautifulsoup urllib2


【解决方案1】:

BeautifulSoup 需要一个 HTML 字符串。你提供的是一个响应对象。

从响应中获取 html:

 html = page.read()

然后把html交给beautifulsoup或者直接传给你喜欢的。

另外 id 建议阅读以下两个链接:

urllib2 documentation

BeautifulSoup documentation

【讨论】:

  • 仍然返回,当我打印汤时,我得到了 HTML,但在搜索表格时,我没有得到任何内容
  • 我回家后看看。 :-)
  • 你试过用python3运行它吗?它对我有用! pastebin.com/1i0Sm1j3
  • 抱歉这么晚才回复:-/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
相关资源
最近更新 更多