如何在scrapy python中编写自定义链接提取器
发布时间:2020-09-21 06:50:42 所属栏目:Python 来源:互联网
导读:我想编写我的自定义scrapy链接提取器来提取链接. scrapy文档说它有两个内置的提取器. http://doc.scrapy.org/en/latest/topics/link-extractors.html 但我还没有看到任何代码示例如何通过自定义链接提取器实现,有人可以给出一些编写自定义提取器的示例吗? 这
|
我想编写我的自定义scrapy链接提取器来提取链接. scrapy文档说它有两个内置的提取器. http://doc.scrapy.org/en/latest/topics/link-extractors.html 但我还没有看到任何代码示例如何通过自定义链接提取器实现,有人可以给出一些编写自定义提取器的示例吗? 解决方法这是自定义链接提取器的示例class RCP_RegexLinkExtractor(SgmlLinkExtractor):
"""High performant link extractor"""
def _extract_links(self,response_text,response_url,response_encoding,base_url=None):
if base_url is None:
base_url = urljoin(response_url,self.base_url) if self.base_url else response_url
clean_url = lambda u: urljoin(base_url,remove_entities(clean_link(u.decode(response_encoding))))
clean_text = lambda t: replace_escape_chars(remove_tags(t.decode(response_encoding))).strip()
links_text = linkre.findall(response_text)
urlstext = set([(clean_url(url),clean_text(text)) for url,_,text in links_text])
return [Link(url,text) for url,text in urlstext]
用法 rules = (
Rule(
RCP_RegexLinkExtractor(
allow=(r"epolls/2012/president/[a-z]{2}/[a-z]+_romney_vs_obama-[0-9]{4}.html"),# Regex explanation:
# [a-z]{2} - matches a two character state abbreviation
# [a-z]* - matches a state name
# [0-9]{4} - matches a 4 number unique webpage identifier
allow_domains=('realclearpolitics.com',),callback='parseStatePolls',# follow=None,# default
process_links='processLinks',process_request='processRequest',)
看看这里https://github.com/jtfairbank/RCP-Poll-Scraper (编辑:日照站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- python – 结束索引为0的切片运算符
- 如何使用Anaconda将IPython4降级为3
- python – 为什么pow(x,y)的时间复杂度为O(1),而
- 如何从Python中的文件中读取多行列表?
- python – Pandas Dataframe查找所有列等于的行
- python – 关于Pandas Dataframe的Kurtosis doen
- `with canvas:`(Python`with something()as x:
- python – syncdb和迁移有什么区别?
- python – 使用PyGtk时,GUI未从其他线程更新
- python – Sorl-thumbnail生成黑色方块而不是图像
热点阅读
