파이썬을 이용해 크롤링을 하는데 페이지가 있는 사이트가 아니라 자동으로 더보기가 되는 페이지를 크롤링하고 싶습니다.
조회수 1447회
import sys from bs4 import BeautifulSoup import re import urllib.request from urllib.parse import quote
TARGET_URL = "https://twitter.com/search?f=tweets&vertical=defaul" TARGET_URL_KEWORD = '&q=' TARGET_URL_REST = '&src=typd'
INPUT_FILE_NAME = 'twt_craw.txt' OUTPUT_FILE_NAME = 'twt_craw_clean.txt'
기사 본문 내용 긁어오기 (위 함수 내부에서 기사 본문 주소 받아 사용되는 함수)
def get_text(URL, output_file): source_code_from_url = urllib.request.urlopen(URL) soup = BeautifulSoup(source_code_from_url, 'lxml', from_encoding='utf-8')
text = ''
for item in soup.find_all('p', {"class", "TweetTextSize js-tweet-text tweet-text"}):
text = str(item.find_all(text=True))
output_file.write(text+"\n")
클리닝 함수
def clean_text(text): cleaned_text = re.sub('[a-zA-Z]', '', text) cleaned_text = re.sub('[0123456789ㅋㅠㅜㅎ{}[]\/?.,;:|)*~`!-_+<>@#$%&\=(\'\"]', '', cleaned_text) return cleaned_text
def main(argv): if len(argv) != 2: print("python [모듈이름] [키워드]") return keyword = argv[1]
URL = TARGET_URL+ TARGET_URL_KEWORD \
+ quote(keyword) + TARGET_URL_REST
output_file = open(INPUT_FILE_NAME, 'w',encoding='utf-8')
get_text(URL, output_file)
output_file.close()
read_file = open(INPUT_FILE_NAME, 'r', encoding='utf-8')
write_file = open(OUTPUT_FILE_NAME, 'w', encoding='utf-8')
text = read_file.read()
text = clean_text(text)
write_file.write(text)
read_file.close()
write_file.close()
if name == 'main': main(sys.argv)
트위터는 url을 보니까 페이지 수에 대한 것이 없어서 검색어 입력시 화면에 보이는 부분만 크롤링됩니다. 트위터는 자동으로 더보기가 되어서 트윗이 계속 생기는데, 더보기 이후의 트윗도 같이 크롤링하고 싶습니다. 어떻게 하면 될까요?
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력