关于 scrapy 的两个问题,求大牛

2015-03-23 10:04:09 +08:00
 suguoguosu

1、原来运行正常的脚本,

start_urls = ['http://quote.eastmoney.com']
rules=(
Rule(LinkExtractor(allow=('/[s][z,h][0,3,6][0]\d{4}.html',)),
callback='parse_news',follow=True),)

当添加如下代码试图抓取js生成内容后,就只能抓取到start_urls一页的内容。

RENDER_HTML_URL = "http://127.0.0.1:8050/render.html"

def start_requests(self):
    print self.start_urls
    for url in self.start_urls:
        body = json.dumps({"url": url, "wait": 0.5})
        headers = Headers({'Content-Type': 'application/json'})
        yield Request(RENDER_HTML_URL, self.parse_news, method="POST",body=body, headers=headers)

2、pipeline怎么筛选中文?目前的code如下,如何修正?

!/usr/bin/env python

-- coding:utf-8 --

from scrapy.exceptions import DropItem

class FilterWordsPipeline(object):
"""A pipeline for filtering out items which contain certain words in their
description"""

def process_item(self, item, spider):

    w = '新股'
    w1 = w.encode('gb2312')
    w2 = w1.decode('gb2312')

    if [w1.decode('gb2312')] in item['name']:
        return item
    else:
        print [w1.decode('gb2312')][3:10]
        raise DropItem("======================drop=====================")
3120 次点击
所在节点    Python
2 条回复
XuTao
2015-03-23 11:54:43 +08:00
1、只能抓取到start_urls一页的内容说明你递归的地方出错了

2、python编码问题,最简单的办法就是都解码成unicode
withrock
2015-03-23 16:43:35 +08:00
>>> s = 'v2ex汉字v2ex'
>>> l = []
>>> for i in s:
... if ord(i) < 0x20 or ord(i) > 0x7E:
... continue
... l.append(i)
...
>>> print ''.join(l)
v2exv2ex

http://git.oschina.net/mktime/scrapy-douban-group
最近我也在玩scrapy,希望能帮到你。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/178731

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX