标签: scraper

无法让Scrapy管道工作

我有使用Scrapy框架编写的蜘蛛.我在使任何管道工作时遇到一些麻烦.我在pipelines.py中有以下代码:

class FilePipeline(object):

    def __init__(self):
        self.file = open('items.txt', 'wb')

    def process_item(self, item, spider):
        line = item['title'] + '\n'
        self.file.write(line)
        return item
Run Code Online (Sandbox Code Playgroud)

我的CrawlSpider子类有这一行来激活这个类的管道.

ITEM_PIPELINES = [
        'event.pipelines.FilePipeline'
    ]
Run Code Online (Sandbox Code Playgroud)

但是当我使用它时

scrapy crawl my_spider
Run Code Online (Sandbox Code Playgroud)

我得到一条线说

2010-11-03 20:24:06+0000 [scrapy] DEBUG: Enabled item pipelines:
Run Code Online (Sandbox Code Playgroud)

没有管道(我认为这是日志应该输出它们的地方).

我已经尝试查看文档,但似乎没有任何完整项目的完整示例,看看我是否遗漏了任何内容.

有关下一步尝试的建议吗?或者在哪里寻找进一步的文件?

python pipeline web-crawler scrapy scraper

8
推荐指数
1
解决办法
6844
查看次数

Scrapy Body Text Only

我试图使用python Scrapy从身体上刮掉文本,但还没有运气.

希望有些学者可以在这里帮助我从<body>标签中抓取所有文本.

python scrapy scraper scrape

7
推荐指数
1
解决办法
7814
查看次数

BeautifulSoup:剥离指定的属性,但保留标记及其内容

我正在尝试'defrontpagify'MS FrontPage生成的网站的html,我正在写一个BeautifulSoup脚本来做它.

但是,我试图从包含它们的文档中的每个标记中剥离特定属性(或列表属性)的部分.代码段:

REMOVE_ATTRIBUTES = ['lang','language','onmouseover','onmouseout','script','style','font',
                        'dir','face','size','color','style','class','width','height','hspace',
                        'border','valign','align','background','bgcolor','text','link','vlink',
                        'alink','cellpadding','cellspacing']

# remove all attributes in REMOVE_ATTRIBUTES from all tags, 
# but preserve the tag and its content. 
for attribute in REMOVE_ATTRIBUTES:
    for tag in soup.findAll(attribute=True):
        del(tag[attribute])
Run Code Online (Sandbox Code Playgroud)

它运行没有错误,但实际上并没有删除任何属性.当我在没有外部循环的情况下运行它时,只需对单个属性进行硬编码(soup.findAll('style'= True),它就可以了.

有人知道这里有问题吗?

PS - 我也不太喜欢嵌套循环.如果有人知道更具功能性的map/filter-ish风格,我很乐意看到它.

python beautifulsoup frontpage scraper web-scraping

7
推荐指数
3
解决办法
8659
查看次数

访问 Metacritic API 和/或 Scraping

有谁知道 Metacritic api 的文档在哪里/它是否仍然有效。https://market.mashape.com/byroredux/metacritic-v2#get-user-details上曾经有一个 Metacritic API,今天消失了。

否则,我试图自己抓取该网站,但一直被 429 慢下来阻止。这个小时我获得了 3 次数据,但在过去 20 分钟内无法获得更多数据,这使得测试变得困难并且应用程序可能无用。如果还有什么我可以做的事情来逃避我不知道的事情,请告诉我。

api scraper scrape

7
推荐指数
1
解决办法
2万
查看次数

如果数据是通过Javascript加载的,如何使用php Goutte和Guzzle进行爬网?

很多时候,当我们遇到问题时,我们会遇到使用Javascript生成页面上呈现的内容的问题,因此scrapy无法为其抓取(例如,ajax请求,jQuery)

php web-crawler scraper goutte guzzle

7
推荐指数
2
解决办法
9220
查看次数

机械化提交表格字符编码问题

我试图抓取http://www.nscb.gov.ph/ggi/database.asp,特别是从选择市/省获得的所有表格.我正在使用python与lxml.html和mechanize.到目前为止,我的刮刀工作正常,但是HTTP Error 500: Internal Server Error在提交市政[19]"Peñarrubia,Abra"时我得到了.我怀疑这是由于字符编码.我的猜测是,烯字符(上面带有波浪号的n)会导致这个问题.我怎样才能解决这个问题?

我脚本的这一部分的一个工作示例如下所示.因为我刚开始使用python(并且经常使用我在SO上找到的片段),所以进一步的评论非常感谢.

from BeautifulSoup import BeautifulSoup
import mechanize
import lxml.html
import csv



class PrettifyHandler(mechanize.BaseHandler):
    def http_response(self, request, response):
        if not hasattr(response, "seek"):
            response = mechanize.response_seek_wrapper(response)
        # only use BeautifulSoup if response is html
        if response.info().dict.has_key('content-type') and ('html' in response.info().dict['content-type']):
            soup = BeautifulSoup(response.get_data())
            response.set_data(soup.prettify())
        return response

site = "http://www.nscb.gov.ph/ggi/database.asp"

output_mun = csv.writer(open(r'output-municipalities.csv','wb'))
output_prov = csv.writer(open(r'output-provinces.csv','wb'))

br = mechanize.Browser()
br.add_handler(PrettifyHandler())


# gets municipality stats
response = br.open(site)
br.select_form(name="form2")
muns = br.find_control("strMunicipality2", type="select").items
# …
Run Code Online (Sandbox Code Playgroud)

python encoding mechanize scraper

6
推荐指数
1
解决办法
2919
查看次数

XPath在两个HTML注释之间进行选择?

我有一个很棒的HTML页面.但我想使用Xpath选择某些节点:

<html>
 ........
<!-- begin content -->
 <div>some text</div>
 <div><p>Some more elements</p></div>
<!-- end content -->
.......
</html>
Run Code Online (Sandbox Code Playgroud)

我可以在<!-- begin content -->使用后选择HTML :

"//comment()[. = ' begin content ']/following::*" 
Run Code Online (Sandbox Code Playgroud)

我也可以在<!-- end content -->使用之前选择HTML :

"//comment()[. = ' end content ']/preceding::*" 
Run Code Online (Sandbox Code Playgroud)

但是,我必须让XPath选择两条评论之间的所有HTML吗?

html ruby xpath nokogiri scraper

6
推荐指数
1
解决办法
1531
查看次数

在rvest中搜索位置数据

我正在尝试从我使用rvest的网址列表中搜索纬度/经度数据.每个网址都有一个包含特定位置的嵌入式谷歌地图,但网址本身并未显示API所采用的路径.

在查看页面源代码时,我看到我所关注的部分在这里:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var myLatlng = new google.maps.LatLng(43.805170,-70.722084);
var myOptions = {
  zoom: 16,
  center: myLatlng,
  mapTypeId: google.maps.MapTypeId.SATELLITE
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

var marker = new google.maps.Marker({
    position: myLatlng, 
    map: map,
    title:"F.E. Wood & Sons - Natural Energy"
});   
Run Code Online (Sandbox Code Playgroud)

现在,如果我可以获得具有LatLng(....)输入的行,我可以使用一些字符串解析操作来导出所有URL的纬度和经度值.

我写了以下代码来获取我的数据:

require(rvest)
require(magrittr)
fetchLatLong<-function(url){
  url<-as.character(url)
  solNum<-html(url)%>%
    html_nodes("#map_canvas")%>%
    html_attr("script")
}
Run Code Online (Sandbox Code Playgroud)

(使用selectorGadget找到"map_canvas"选择器;您可以在此处查看整个源).

我有最糟糕的时间来阅读我所追求的内容.我尝试了很多节点和节点组合,但无济于事.我玩过phantom.js,但问题是它不是js渲染的html内容我正在追求:我正在寻找API查询输入,它被写入页面代码(或者,至少,我的业余眼睛似乎是).

有人有建议吗?

javascript r scraper web-scraping rvest

5
推荐指数
1
解决办法
1244
查看次数

如何操作从X射线刮刀(node.js)检索的默认值

这是我的代码:

var Xray = require('x-ray');  
var x = Xray();
x('http://someurl.com', 'tr td:nth-child(2)', [{  
    text: 'a',
    url: 'a@href'
  }]).write('results.json')
Run Code Online (Sandbox Code Playgroud)

我需要使用每个标记中的第一个单词填充名为"text"的字段.标记值的示例:

"FirstWord SecondWord ThirdWord"

实际结果是文本:FirstWord SecondWord ThirdWord

期望的结果文本:FirstWord

我可以对result.json文件进行后处理,但我不喜欢这样.

javascript scraper node.js

5
推荐指数
1
解决办法
745
查看次数

http请求的for循环延迟

我刚刚开始使用 JS 和 Node.js。我正在尝试使用 Node.js 和一些模块构建一个简单的刮板作为第一个项目,例如requestcheerio。我想在数组中包含的每个域的每个 http 请求之间添加 5 秒延迟。你能解释一下怎么做吗?

这是我的代码:

var request = require('request');

var arr = [ "http://allrecipes.com/", "http://www.gossip.fr/" ];

for(var i=0; i < arr.length; i++) {
    request(arr[i], function (error, response, body){
        console.log('error:', error);
        console.log('statusCode:', response && response.statusCode);
        console.log('body:', body);
    });
}
Run Code Online (Sandbox Code Playgroud)

url loops scraper node.js

5
推荐指数
2
解决办法
6019
查看次数