【问题标题】:Can I fill web forms with Scrapy?我可以用 Scrapy 填写 Web 表单吗?
【发布时间】:2014-03-22 16:12:00
【问题描述】:

现在我正在使用 iMacros 从网络中提取数据并填写提交数据的表单。

但 iMacros 是一个昂贵的工具。我需要一个免费的库,并且我已经阅读了有关 Scrapy 进行数据挖掘的信息。我用它编程有点复杂,但金钱规则。

问题是我是否可以用 Scrapy 填写 html 表单并提交到网页。我不想使用 Javascript,我只想使用 Python 脚本。

我在http://doc.scrapy.org/ 中进行了搜索,但没有找到关于表单提交的任何信息。

【问题讨论】:

  • 这是一个网络爬虫,而不是一个网络请求库。实际上就在 5 分钟前从另一个问题中获得了这个链接。尝试:requests.readthedocs.org/en/latest
  • 谢谢,我去看看。 web表单使用POST方式,我也需要上传文件。
  • 没关系。仍然无法使用scrapy :P 其他所有不用于抓取的库都可以:)
  • iMacros 一点也不贵。 FireFox 插件对大多数人来说已经足够好了。你只需要编写一些 JavaScript 脚本。
  • @macroscripts iMacros 很酷,脚本接口(API)非常强大且易于使用,但是企业版要995美元(唯一支持脚本接口的,我真的需要它)。我去年购买了许可证,但几个月后我将过期,我正在寻找替代解决方案。

标签: python web-scraping scrapy form-submit


【解决方案1】:

使用scrapy.http.FormRequest 类。

FormRequest 类通过处理 HTML 表单的功能扩展了基本请求

http://doc.scrapy.org/en/latest/topics/request-response.html#formrequest-objects

【讨论】:

    【解决方案2】:

    Mechanize 是一个 python 库,可让您自动与网站交互。支持HTML表单填写。

    【讨论】:

    • 我需要 Python 3 支持。我正在尝试使用Requests
    • 按照 surfer190 的建议使用 scrapy。或者...尝试 Selenium(使用 HTMLUNIT 驱动程序无头)
    【解决方案3】:

    以下程序向您解释如何填写表格:

    import mechanize
    import cookielib
    from BeautifulSoup import BeautifulSoup
    import html2text
    # Browser
    br = mechanize.Brenter code hereowser()
    
    # Cookie Jar
    cj = cookielib.LWPCookieJar()
    br.set_cookiejar(cj)
    
    # Browser options
    br.set_handle_equiv(True)
    br.set_handle_gzip(True)
    br.set_handle_redirect(True)
    br.set_handle_referer(True)
    br.set_handle_robots(False)
    
    # Follows refresh 0 but not hangs on refresh > 0
    br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
    
    # User-Agent (this is cheating, ok?)
    br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
    
    # The site we will navigate into, handling it's session
    br.open('http://gmail.com')
    
    # Select the first (index zero) form
    br.select_form(nr=0)
    
    # User credentials
    br.form['Email'] = 'user'
    br.form['Passwd'] = 'password'
    # Login
    br.submit()
    
    # Filter all links to mail messages in the inbox
    all_msg_links = [l for l in br.links(url_regex='\?v=c&th=')]
    # Select the first 3 messages
    for msg_link in all_msg_links[0:3]:
        print msg_link
        # Open each message
        br.follow_link(msg_link)
        html = br.response().read()
        soup = BeautifulSoup(html)
        # Filter html to only show the message content
        msg = str(soup.findAll('div', attrs={'class': 'msg'})[0])
        # Show raw message content
        print msg
        # Convert html to text, easier to read but can fail if you have intl
        # chars
    #   print html2text.html2text(msg)
        print
        # Go back to the Inbox
        br.follow_link(text='Inbox')
    
    # Logout
    br.follow_link(text='Sign out') 
    

    【讨论】:

    • 我猜这与 Scrapy 无关。
    猜你喜欢
    • 2016-05-18
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多