python - 文本文件中的Scrapy start_urls

标签 python scrapy web-crawler

我正在尝试抓取网址并检索每个网址的h1。 url 存储在文本文件中。代码是:

class MySpider(CrawlSpider):
    name = "sitemaplocation"
    allowed_domains = ["xyz.nl"]
    f = open("locationlist.txt",'r')
    start_urls = [url.strip() for url in f.readlines()]
    f.close()


def parse(self, response):
    sel = Selector(response)

    title= sel.xpath("//h1[@class='no-bd']/text()").extract()
    print title

代码在网站中爬行,但不打印任何内容。任何帮助都会有用。

最佳答案

尝试放置此:

name = "sitemaplocation"
allowed_domains = ["xyz.nl"]
f = open("locationlist.txt",'r')
start_urls = [url.strip() for url in f.readlines()]
f.close()

进入

__init__

MySpider 类中的方法。

你在哪里调用解析函数?

关于python - 文本文件中的Scrapy start_urls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22960140/

相关文章:

python - Pygame 碰撞检查错误

python - 如何在python的scrapy中删除字符串的特定部分

Scrapy,在管道中发出 http 请求

django - 从 Heroku 上托管的 Django 项目的子目录运行非 Django 命令?

python - 使用 XPath 在使用 python 的 <td> 单元格中获取文本

go - 如何正确使用选择器

Python、图像压缩和多重处理

python - 如何检测实心圆网格?

python - 在 Seaborn Barplot 上标记轴

python - 使用 Scrapy 匹配 robots.txt 中的多个用户代理