【发布时间】:2014-05-01 03:19:14
【问题描述】:
在第 29 行出现语法错误,如下所示:links = parsed_body.xpath('//div[contains(@class, "b-thumb-128px")]/a/@href')] .我已经为目标站点编写了有效的 xpath,所以我不确定为什么或如何解决这个特殊错误。
import requests
from lxml import html
from pprint import pprint
from urlparse import urljoin
from thready import threaded
import os
import math
import csv
CACHE_DIR = os.path.join(os.path.dirname(__file__), 'wanpy')
def get_links():
STARTING_URL = 'http://example.com/en/search/?h=3&k=&p=1&sid=wan'
results_per_page = 60
response = requests.get(STARTING_URL)
dive = html.fromstring(response.text)
div = dive.xpath("//div[contains(@class, 'b-tabs-utility')]")[0].text
last_pg = math.ceil(int(div.split()[-2]) / results_per_page)
BASE_URL = 'http://example.com/en/search/?h=3&k=&p=%d&sid=wanboo'
urls = []
for i in xrange(last_pg):
response = requests.get(BASE_URL % i)
parsed_body = html.fromstring(response.text)
links = parsed_body.xpath('//div[contains(@class, "b-thumb-128px")]//a/@href')]
for link in links:
urls.append(link)
threaded(urls, scrape_inventory, num_threads=10)
def scrape_inventory():
with open("data/wan.csv", "w") as f:
fieldnames = ("model", "title", "description", "price", "image","additional_image", "scrape_url")
output = csv.writer(f, delimiter="\t")
output.writerow(fieldnames)
print "scraping %s ..." % url
response = requests.get(url)
parsed_body = html.fromstring(response.text)
name = re.sub(r'\D\W\S', "", parsed_body.xpath("//h1[contains(@class, 'b-ttl-main')]/text()"))
#description = re.sub(r'\D\W\S', "", parsed_body.xpath("//div[contains(@class, 'b-container b-editable')]/text()"))
price = re.sub(r'\D\W\S', "", round(float(parsed_body.xpath("//span[contains(@class, 'b-text-xxlarge b-text-prime')]/text()")) * 2 + 15), 2)
output.writerow([name, price])
if __name__ == '__main__':
get_links()
【问题讨论】:
-
“语法错误”不如提供特定语法错误有用。 XPath 语法错误和 Python 语法错误是完全不同的事情。
-
此外,在发布之前尝试将代码减少到重现问题所需的最低限度是一种很好的方式。
标签: syntax-error lxml python-requests