【问题标题】:python bs4 scripts work on command prompt and not ubuntupython bs4脚本在命令提示符下工作,而不是ubuntu
【发布时间】:2020-09-15 04:15:01
【问题描述】:

我有一个用 bs4 和 requests 制作的非常基本的网络爬虫,当我在命令提示符下运行它时它工作得非常好,但是当我尝试在 ubuntu 上运行它时它不会返回任何东西。

def scrape():
    queryURL = "https://www.google.com/search?q=oranges"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
    }
    r = requests.get(queryURL, headers=headers)
    soup = BeautifulSoup(r.text, "html.parser")
    a = soup.find_all('div', class_='r')

    if r.status_code == 200:
        soup = BeautifulSoup(r.text, "html.parser")
        urls = []
        for result in soup.find_all('div', class_='r'):
            anchors = result.find_all('a')
            if anchors:
                link = anchors[0]['href']
                urls.append(link)
        for url in urls:
            print(url)
scrape()

On command prompt

On ubuntu

【问题讨论】:

  • 编辑代码并使用特殊按钮{} 格式化代码。你有错误的缩进。
  • 使用 print 查看你在变量中得到了什么。谷歌不喜欢脚本,它有很多钱来创建复杂的系统来识别机器人/脚本并阻止它们 - 当你从某个 IP 运行它或者你过于频繁地运行它时,它可能会阻止你的脚本,等等。
  • 我确实使用了我复制和粘贴的 {}。它只是不会缩进它
  • 粘贴代码后必须使用特殊按钮{} - 它会在所有行中添加4个空格,然后正确显示。

标签: python python-3.x python-2.7 ubuntu beautifulsoup


【解决方案1】:

我已经通过使用住宅代理解决了这个问题。

【讨论】:

    猜你喜欢
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    相关资源
    最近更新 更多