SMP*_*GRP 1 python recursion web-crawler
我正在使用scrapy,我想通过www.rentler.com.我去了网站搜索了我感兴趣的城市,这里是搜索结果的链接:
https://www.rentler.com/search?Location=millcreek&MaxPrice=
Run Code Online (Sandbox Code Playgroud)
现在,我感兴趣的所有列表都包含在该页面上,我希望逐个递归地逐步执行它们.
每个列表都列在:
<body>/<div id="wrap">/<div class="container search-res">/<ul class="search-results"><li class="result">
Run Code Online (Sandbox Code Playgroud)
每个结果都有一个 <a class="search-result-link" href="/listing/288910">
我知道我需要为crawlspider创建一个规则并让它查看该href并将其附加到url.这样它就可以进入每个页面,并获取我感兴趣的数据.
我想我需要这样的东西:
rules = (Rule(SgmlLinkExtractor(allow="not sure what to insert here, but this is where I think I need to href appending", callback='parse_item', follow=true),)
Run Code Online (Sandbox Code Playgroud)
更新 *感谢您的输入.这是我现在拥有的,它似乎运行但不刮:*
import re
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from KSL.items import KSLitem
class KSL(CrawlSpider):
name = "ksl"
allowed_domains = ["https://www.rentler.com"]
start_urls = ["https://www.rentler.com/ksl/listing/index/?sid=17403849&nid=651&ad=452978"]
regex_pattern = '<a href="listing/(.*?) class="search-result-link">'
def parse_item(self, response):
items = []
hxs = HtmlXPathSelector(response)
sites = re.findall(regex_pattern, "https://www.rentler.com/search?location=millcreek&MaxPrice=")
for site in sites:
item = KSLitem()
item['price'] = site.select('//div[@class="price"]/text()').extract()
item['address'] = site.select('//div[@class="address"]/text()').extract()
item['stats'] = site.select('//ul[@class="basic-stats"]/li/div[@class="count"]/text()').extract()
item['description'] = site.select('//div[@class="description"]/div/p/text()').extract()
items.append(item)
return items
Run Code Online (Sandbox Code Playgroud)
思考?
如果你需要从html文件中删除数据,就是这种情况,我建议使用BeautifulSoup,它很容易安装和使用:
from bs4 import BeautifulSoup
bs = BeautifulSoup(html)
for link in bs.find_all('a'):
if link.has_attr('href'):
print link.attrs['href']
Run Code Online (Sandbox Code Playgroud)
这个小脚本将获得HTML标记href
内的所有内容a
.
编辑:功能齐全的脚本:
我在我的计算机上对此进行了测试,结果与预期一致,BeautifulSoup需要纯HTML,你可以从中获取所需内容,看看这段代码:
import requests
from bs4 import BeautifulSoup
html = requests.get(
'https://www.rentler.com/search?Location=millcreek&MaxPrice=').text
bs = BeautifulSoup(html)
possible_links = bs.find_all('a')
for link in possible_links:
if link.has_attr('href'):
print link.attrs['href']
Run Code Online (Sandbox Code Playgroud)
这只会告诉你如何从你想要刮掉的html页面中抓取href,当然你可以在scrapy中使用它,正如我告诉你的那样,BeautifulSoup只需要简单的HTML,这就是为什么我使用requests.get(url).text
你可以刮掉那.所以我猜scrapy可以将那个简单的HTML传递给BeautifulSoup.
编辑2 好的,看起来我认为你根本不需要scrapy,所以如果以前的脚本为你提供了你想要从作品中获取数据的所有链接,你只需要这样做:
假设我有一个有效的网址列表,我想从中获取特定数据,比如价格,英亩,地址......您可以只使用之前的脚本而不是打印网址到屏幕,您可以将它们附加到列表并仅附加那些开头的/listing/
.这样你就有了一个有效的网址列表.
for url in valid_urls:
bs = BeautifulSoup(requests.get(url).text)
price = bs.find('span', {'class': 'amount'}).text
print price
Run Code Online (Sandbox Code Playgroud)
您只需要查看源代码,就可以了解如何从每个URL中获取所需的数据.