0
은 내가 각 사용자의 웹 페이지에서 내용을 추출하기위한 XPath를 사용하고 등 최신주기 구문 오류 - XPath를
을, 사용자 이름, upvotes 같은 몇 가지 세부 사항을 추출 가입 Scrapy 크롤러를 사용하고 있습니다.
코드 :
import scrapy
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from scrapy.spiders import BaseSpider
from scrapy.http import FormRequest
from loginform import fill_login_form
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
class UserSpider(scrapy.Spider):
name = 'userspider'
start_urls = ['http://forum.nafc.org/login/']
#Getting the list of usernames
user_names = ['Bob', 'Tom'] #List of Usernames
def __init__(self, *args, **kwargs):
super(UserSpider, self).__init__(*args, **kwargs)
def parse(self, response):
return [FormRequest.from_response(response,
formdata={'registerUserName': 'user', 'registerPass': 'password'},
callback=self.after_main_login)]
def after_main_login(self, response):
for user in self.user_names:
user_url = 'profile/' + user
yield response.follow(user_url, callback=self.parse_user_pages)
def parse_user_pages(self, response):
yield{
"USERNAME": response.xpath('//div[contains(@class, "main") and contains(@class, "no-sky-main")]/h1[contains(@class, "thread-title")]/text()').extract_first()
"UPVOTES": response.xpath('//div[contains(@class, "proUserInfoLabelLeft") and @id="proVotesCap"]/text()').extract()[0]
}
if __name__ == "__main__":
spider = UserSpider()
P.S. Scrapy Shell에서 XPath의 구문을 수동으로 확인했는데 제대로 작동했습니다.
코드에 눈에 띄지 않는 것이 있습니까?
감사합니다! 나는 지적하면서 어리석은 실수를했습니다! –