웹 사이트를 재귀 적으로 크롤링해야합니다. 이것이 내가 지금 가지고있는 것입니다.
class DemoSpider(CrawlSpider):
name = 'sample_recursive'
allowed_domains = ['www.example.org']
start_urls = [
"http://example.org"
]
rules = [Rule(SgmlLinkExtractor(allow=(r'/.org/site/ID/[\w*\W*]*'), deny=(r'.org/subscription')), callback='parse_start_url', follow=True)]
def parse_start_url(self, response):
items = []
item = DemoSampleItem()
item["source_url"] = response.url
item["title"] = response.xpath('//div[@class="content-title"]/h2/text()')[0].extract()
item["breadcrumb"] = response.xpath("//ul[@class='breadcrumbs']")[0].extract()
item["content"] = response.xpath("//div[@class='main_col']")[0].extract()
item["right_col"] = response.xpath("//div[@class='right_col']").extract()
item["left_col"] = response.xpath("//div[@class='left_col']")[0].extract()
item["depth"] = response.meta.get('depth', 0)
items.append(item)
return items
"example.org", "example.org/site/ID/home", "example.org/site/ID/Partners"및 "example.org/site/ID/home/과 같은 페이지를 긁어 내고 싶습니다. our-values "를 입력 한 다음 각 항목 파이프 라인을 자체 항목으로 mysql db에 저장합니다.
class AcdiSamplePipeline(object):
# connect
db_connection = MySQLdb.connect(host='localhost', user='user', passwd='passwd', db='dbname'
, charset='utf8', use_unicode=True)
# create a database cursor
cursor = db_connection.cursor()
def process_item(self, item, spider):
source_url = item["source_url"]
title = item["title"].encode('utf-8')
breadcrumb = item["breadcrumb"].encode('utf-8')
content = item["content"].encode('utf-8')
left_col = item["left_col"].encode('utf-8')
right_col = item["right_col"].encode('utf-8')
depth = item["depth"]
try:
self.cursor.execute("""INSERT INTO table_name (source_url, title, breadcrumb, content
, right_col, left_col, page_depth)
VALUES (%s, %s, %s, %s, %s, %s, %s)""",
(source_url
, title
, breadcrumb
, content
, right_col
, left_col
, depth))
self.db_connection.commit()
except MySQLdb.Error, e:
print("--------------- Printing DB ERROR(s) -------------------")
print "ERROR WHILE DB WRITE %d: %s" % (e.args[0], e.args[1])
return item
그러나 현재로서는 데이터베이스에서 "example.org"를 스크래핑하고 저장하는 유일한 방법입니다. 왜 재귀 적으로 사이트를 통과하지 않는지 아십니까?
LinkExtractor의 정규식이 잘못된 것 같습니다.
allow=(r'/.org/site/ID/[\w*\W*]*')
# ^--- this slash doesn't belong here.
# Plus, the dot should be escaped, else it matches any character
정규식을 더 좋아하는 것 같습니다.
allow=(r'[.]org/site/ID/.+')
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다