-1
다음과 같은 구문 오류가 29 번 발생합니다. links = parsed_body.xpath ('// div [@class, "b-thumb-128px"]]/a/@ href ')]. 나는이 paticular 오류를 수정하는 이유 또는 방법에 대해 확실하지 않으므로 대상 사이트에 유효한 xpath를 작성했습니다.python lxml이고 구문 오류가 발생했습니다.
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 구문의 오류는 매우 다릅니다. –
또한 게시하기 전에 문제를 재현하는 데 필요한 최소한의 코드를 줄이는 것이 좋습니다. –