【问题标题】:Unable to get list of URLs无法获取 URL 列表
【发布时间】:2016-01-23 04:48:43
【问题描述】:

我正在尝试使用以下脚本。为什么它没有检索此站点的 URL 列表?它适用于其他网站。

最初我认为问题是 robots.txt 不允许,但是当我运行它时它没有返回错误。

import urllib
from bs4 import BeautifulSoup
import urlparse
import mechanize

url = "https://www.danmurphys.com.au"

br = mechanize.Browser()
br.set_handle_robots(False)

urls = [url]
visited =[url]

print 
while len(urls)>0:
try:
    br.open(urls[0])
    urls.pop(0) 
    for link in br.links():
        #print link
        #print "The base url is :" + link.base_url # just check there is this applicable to all sites.
        #print "The url is: " + link.url # This gives generally just the page name
        new_url = urlparse.urljoin(link.base_url,link.url)
        b1 = urlparse.urlparse(new_url).hostname
        b2 = urlparse.urlparse(new_url).path
        new_url = "http://"+ b1 + b2

        if new_url not in visited and urlparse.urlparse(url).hostname in new_url:
            visited.append(new_url)
            urls.append(new_url)
            print new_url
except:
    print "error"
    urls.pop(0)

【问题讨论】:

    标签: python web-scraping mechanize


    【解决方案1】:

    您需要使用其他东西来抓取该 URL,例如 scrapyscrapyJSPhantom JS,因为 Mechanize 库不适用于 Javascript。

    r = br.open(urls[0])
    html = r.read()
    print html
    

    你会看到输出:

    <noscript>Please enable JavaScript to view the page content.</noscript>
    

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多